// pages/goTest/goTest.js
const app = getApp()
var base_url = app.globalData.url
var template = require('../../template/template.js');
var encryptedData = ""
var iv = ""
var session_key = ''
var auth = 0
var student_id = 0
var teacher_id = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: "33%",
    home: {
      "current": 0,
      "pagePath": "/pages/home/ptwords/ptwords",
      "iconPath": "/images/icons/icona_a.png",
      "text": "习字"
    },
    photo: {
      "current": 0,
      "pagePath": "/pages/photo/goTest/goTest",
      "iconPath": "/images/icons/icon1_1.png",
      "text": "评测"
    },
    mine: {
      "current": 0,
      "pagePath": "/pages/my/visitor/mine/mine",
      "iconPath": "/images/icons/icon4_1.png",
      "text": "我的"
    },
    find: {
      "current": 0,
      "pagePath": "/pages/find/find",
      "iconPath": "/images/icons/icon3.png",
      "text": "班级"
    },
    store: {
      "current": 0,
      "pagePath": "/pages/store/storeHome/storeHome",
      "iconPath": "/images/icons/store_1.png",
      "text": "助学"
    },
    display:'none',
    displayT: "block",
    loginM: "none",
    loginP:"block"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'auth',
      success: function(res) {

        if(res['data']==1){
          that.setData({
            mine:{
              "current": 0,
              "pagePath": "/pages/my/teacher/mine/mine",
              "iconPath": "/images/icons/icon4_1.png",
              "text": "我的"
            },
            // width: "25%",
            display: 'block',
            displayT: "none",
          })
        }else{
          that.setData({
            mine: {
              "current": 0,
              "pagePath": "/pages/my/visitor/mine/mine",
              "iconPath": "/images/icons/icon4_1.png",
              "text": "我的"
            },
            // width: "25%",
            display: 'none',
            displayT: "block",
            // display: 'none'
          })
        }
      },
      fail: function (res) {
        that.setData({
          mine: {
            "current": 0,
            "pagePath": "/pages/my/noauth/noauth",
            "iconPath": "/images/icons/icon4_1.png",
            "text": "我的"
          }
        })
      }
    })  
    wx.getSystemInfo({
      
      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        that.setData({
          height: clientHeight
        });
      }
    });
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          loginM: "none"
        })
      },
      fail: function (res) {
        console.log("getUserInfo:", res)
        that.setData({
          loginM: "block"
        })
      }
    })
    wx.getStorage({
      key: 'auth',
      success: function (res) {

        auth = res['data']
        console.log("auth:", auth)
      },
    })
    wx.getStorage({
      key: 'student_id',
      success: function (res) {

        student_id = res['data']
        console.log("student_id:", student_id)
      },
    })
    wx.getStorage({
      key: 'teacher_id',
      success: function (res) {
        teacher_id = res['data']
        console.log("teacher_id:", teacher_id)
      },
    })  
    wx.getStorage({
      key: 'phoneNumber',
      success: function (res) {

      that.setData({
        loginP:"none"
      })
      },fail:function(res){
        that.setData({
          loginP: "block"
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  gotoPhoto: function () {
    wx.navigateTo({
      url: '../toPhoto/toPhoto',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      //用户按了允许授权按钮

      that.setData({
        loginM: "none"
      })
      that.onLoad()

    } else {
      that.setData({
        loginM: "none"
      })
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {

        }
      })
    }
  },
  getPhoneNumber:function(e){
    var that = this;
    console.log(e.detail.errMsg == "getPhoneNumber:ok");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      console.log("phoneData:", e.detail)
      that.setData({
        loginP: "none"
      })
      var that = this;
      





      wx.login({
        success: function (res) {
          var code = ''
          code = res.code
          //获取openid
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data: {
              appid: 'wx832b5cf39f1ba743',
              secret: '352b46c1077186a3dc391f34c7da1fad',
              js_code: code,
              grant_type: 'authorization_code'
            },
            success: function (res) {

              var encryptedData = ''
              var iv = ''
              session_key = res.data.session_key
              console.log(res)
              wx.getUserInfo({
                success: function (res) {
                  encryptedData = res.encryptedData
                  iv = res.iv
                  wx.request({
                    url: base_url + '/getPhone/',
                    method: 'POST',
                    data: {
                      session_key: session_key,
                      encryptedData: e.detail.encryptedData,
                      iv: e.detail.iv,
                      auth:auth,
                      student_id: student_id,
                      teacher_id:teacher_id
                    },
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function (res) {
                      console.log(res["phoneNumber"])
                      wx.setStorage({
                        key: 'phoneNumber',
                        data: res["phoneNumber"],
                        success: function () {
                          console.log(res)
                        }
                      })
                    }
                  })

                },
              })

            }
          })

        }
      })
    }else{
      that.setData({
        loginP: "block"
      })
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {

        }
      })
    }
    
  }
})